package com.wbao.dianniu.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.alibaba.sdk.android.media.utils.StringUtils;
import com.arlen.photo.photopickup.util.FileSizeUtil;
import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.ClipImageLayout;
import com.wbao.dianniu.logical.Const;

public class AdImageCutActivity extends BaseActivity {
    private ClipImageLayout mClipImageLayout;
    private Button rightBtn;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_cut_ad_picture, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName("裁剪");

        mClipImageLayout = (ClipImageLayout) findViewById(R.id.cut_ad_picture);
        rightBtn = getRightTextButton();
        rightBtn.setText("裁剪");
//		rightBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_clip_icon));
        rightBtn.setOnClickListener(this);
        intent = getIntent();
        String pathImage = intent.getStringExtra(Const.BUNDLE_DATA);
        if (null != pathImage) {
            Bitmap bitmap = BitmapFactory.decodeFile(pathImage);
            mClipImageLayout.setBitmap(bitmap);
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.right_button:
                Bitmap bitmap = mClipImageLayout.clip();
                // 图片存储到缓存中
                String headPic = "image_" + StringUtils.getUUID();
                String cachePath = FileSizeUtil.compressBitmapToCache(this, Bitmap.CompressFormat.JPEG, headPic, bitmap);
                //把缓存中的图片上传到cdn 保存
                Intent intent = getIntent();
//			// 把返回数据存入Intent
                intent.putExtra("cachePath", cachePath);
                intent.putExtra("headPic", headPic);
                // 设置返回数据
                setResult(RESULT_OK, intent);
                this.finish();
                break;
            default:
                break;
        }
    }

}
